//------------Sonic CD BossLegJoint Script-------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

//-------Aliases-------//
#alias 48: TYPE_BOSSLEGJOINT

// Function declarations
#function BossLegJoint_Function59
#function BossLegJoint_Function60
#function BossLegJoint_Function61

function BossLegJoint_Function59
	ArrayPos0=Object.Value0
	Object.XPos=Object[ArrayPos0].XPos
	Object.XPos+=786432
	Object.XPos+=temp7
	if Object.DrawOrder==2
		Object.XPos-=655360
	endif
	Object.YPos=Object[ArrayPos0].YPos
	Object.YPos+=1310720
	Object.YPos+=temp6
endfunction


function BossLegJoint_Function60
	Sin256(temp6,Object.Value2)
	temp6<<=12
	Cos256(temp7,Object.Value2)
	temp7<<=12
	GetBit(temp0,Object.Value4,4)
	if temp0==0
		SetBit(Object.Value4,1,0)
		CallFunction(BossLegJoint_Function59)
	else
		GetBit(temp0,Object.Value4,5)
		if temp0==0
			CallFunction(BossLegJoint_Function59)
		else
			GetBit(temp0,Object.Value4,1)
			if temp0==0
				SetBit(Object.Value4,1,1)
				Object.Value6=temp6
				Object.Value7=temp7
			else
				ArrayPos0=Object.Value0
				ArrayPos1=Object[ArrayPos0].Value0
				temp0=temp7
				temp0-=Object.Value7
				Object.Value7=temp7
				Object[ArrayPos0].XPos-=temp0
				Object[ArrayPos1].XPos-=temp0
				temp0=temp6
				temp0-=Object.Value6
				Object.Value6=temp6
				Object[ArrayPos0].YPos-=temp0
				Object[ArrayPos1].YPos-=temp0
			endif
		endif
	endif
endfunction


function BossLegJoint_Function61
	ArrayPos0=Object.Value1
	ArrayPos1=Object[ArrayPos0].Value1
	if Object[ArrayPos1].State==2
		if Object.Value2>=88
			GetBit(temp0,Object[ArrayPos1].Value4,0)
			if temp0==1
				SetBit(Object[ArrayPos1].Value4,0,0)
				SetBit(Object.Value4,0,1)
			endif
			CallFunction(BossLegJoint_Function60)
		endif
	else
		Object[ArrayPos1].State=1
		if Object.Value2<88
			Object.Value2+=Object.Value5
		endif
		CallFunction(BossLegJoint_Function60)
	endif
endfunction


event ObjectMain
	switch Object.State
	case 0
		Object.Value2=88
		Object.Value5=2
		Object.State=6
		break
	case 1
		ArrayPos0=Object.Value1
		ArrayPos1=Object[ArrayPos0].Value1
		if Object[ArrayPos1].State!=3
			if Object[ArrayPos1].State!=4
				SetBit(Object[ArrayPos1].Value4,0,0)
				Object[ArrayPos1].State=3
			endif
		endif
		temp0=0
		if Object.Value2>0
			Object.Value2-=Object.Value5
			if Object.Value2>0
				CallFunction(BossLegJoint_Function60)
				temp0=1
			endif
		endif
		if temp0==0
			Object.Value2=0
			if Object[ArrayPos1].State!=4
				GetBit(temp0,Object[ArrayPos1].Value4,0)
				if temp0==0
					CallFunction(BossLegJoint_Function60)
				else
					SetBit(Object[ArrayPos1].Value4,0,0)
					Object.State=2
					CallFunction(BossLegJoint_Function60)
				endif
			else
				Object.State=2
				CallFunction(BossLegJoint_Function60)
			endif
		endif
		break
	case 2
		ArrayPos0=Object.Value1
		ArrayPos1=Object[ArrayPos0].Value1
		Object.Value2+=Object.Value5
		if Object.Value2>=88
			Object.Value2=88
			Object.State=3
			Object[ArrayPos1].State=1
		endif
		CallFunction(BossLegJoint_Function60)
		break
	case 3
		ArrayPos0=Object.Value1
		ArrayPos1=Object[ArrayPos0].Value1
		GetBit(temp0,Object[ArrayPos1].Value4,4)
		if temp0==1
			SetBit(Object.Value4,0,1)
		endif
		CallFunction(BossLegJoint_Function60)
		break
	case 4
		GetBit(temp0,Object.Value4,0)
		if temp0==0
			Object.Value2-=Object.Value5
			if Object.Value2<24
				Object.Value2=24
				SetBit(Object.Value4,0,1)
			endif
		endif
		CallFunction(BossLegJoint_Function60)
		break
	case 5
		ArrayPos0=Object.Value1
		ArrayPos1=Object[ArrayPos0].Value1
		if Object[ArrayPos1].State==4
			if Object.Value2==0
				GetBit(temp0,Object[ArrayPos1].Value4,0)
				if temp0==1
					SetBit(Object[ArrayPos1].Value4,0,0)
					SetBit(Object.Value4,0,1)
				endif
				CallFunction(BossLegJoint_Function60)
			else
				if Object.Value2!=0
					Object.Value2-=Object.Value5
				endif
				CallFunction(BossLegJoint_Function60)
			endif
		else
			Object[ArrayPos1].State=3
			if Object.Value2!=0
				Object.Value2-=Object.Value5
			endif
			CallFunction(BossLegJoint_Function60)
		endif
		break
	case 6
		CallFunction(BossLegJoint_Function61)
		break
	case 7
		if Object.Value2<24
			Object.Value2+=Object.Value5
			ArrayPos0=Object.Value1
			ArrayPos1=Object[ArrayPos0].Value1
			SetBit(Object[ArrayPos1].Value4,7,1)
			ArrayPos0=Object.Value0
			ArrayPos1=Object[ArrayPos0].Value2
			ArrayPos0=Object[ArrayPos1].Value1
			ArrayPos1=Object[ArrayPos0].Value1
			SetBit(Object[ArrayPos1].Value4,7,1)
			CallFunction(BossLegJoint_Function61)
		else
			if Object.Value2>24
				Object.Value2-=Object.Value5
				ArrayPos0=Object.Value1
				ArrayPos1=Object[ArrayPos0].Value1
				if Object[ArrayPos1].State==4
					if Object.Value2==0
						GetBit(temp0,Object[ArrayPos1].Value4,0)
						if temp0==1
							SetBit(Object[ArrayPos1].Value4,0,0)
							SetBit(Object.Value4,0,1)
						endif
						CallFunction(BossLegJoint_Function60)
					else
						if Object.Value2!=0
							Object.Value2-=Object.Value5
						endif
						CallFunction(BossLegJoint_Function60)
					endif
				else
					Object[ArrayPos1].State=3
					if Object.Value2!=0
						Object.Value2-=Object.Value5
					endif
					CallFunction(BossLegJoint_Function60)
				endif
			else
				ArrayPos0=Object.Value1
				ArrayPos1=Object[ArrayPos0].Value1
				GetBit(temp0,Object[ArrayPos1].Value4,0)
				if temp0==1
					SetBit(Object.Value4,0,1)
				endif
				CallFunction(BossLegJoint_Function60)
			endif
		endif
		break
	case 8
		ArrayPos0=Object.Value0
		ArrayPos1=Object[ArrayPos0].Value1
		Object.XPos=Object[ArrayPos1].XPos
		Object.XPos-=655360
		Object.YPos=Object[ArrayPos1].YPos
		Object.Value2=Object[ArrayPos1].Value2
		break
	case 9
		ArrayPos0=Object.Value1
		ArrayPos1=Object[ArrayPos0].Value1
		temp1=0
		if Object[ArrayPos1].State!=3
			if Object[ArrayPos1].State!=4
				SetBit(Object[ArrayPos1].Value4,0,0)
				Object[ArrayPos1].State=3
			else
				if Object.Value2!=32
					Object.Value2-=Object.Value5
					if Object.Value2>0
						temp1=1
					endif
				endif
				if temp1==0
					Object.Value2=32
					SetBit(Object[ArrayPos1].Value4,0,0)
					Object.State=10
				endif
			endif
		endif
		CallFunction(BossLegJoint_Function60)
		break
	case 10
		ArrayPos0=Object.Value1
		ArrayPos1=Object[ArrayPos0].Value1
		Object.Value2+=Object.Value5
		if Object.Value2>=48
			Object.Value2=48
			Object.State=3
			Object[ArrayPos1].State=1
		endif
		CallFunction(BossLegJoint_Function60)
		break
	case 11
		GetBit(temp0,Object.Value4,0)
		if temp0==0
			ArrayPos0=Object.Value1
			ArrayPos1=Object[ArrayPos0].Value1
			if Object[ArrayPos1].State!=1
				if Object[ArrayPos1].State!=2
					Object[ArrayPos1].State=1
				else
					Object.Value2+=Object.Value5
					if Object.Value2>=80
						SetBit(Object.Value4,0,1)
					endif
				endif
			endif
		endif
		CallFunction(BossLegJoint_Function60)
		break
	case 12
		GetBit(temp0,Object.Value4,4)
		if temp0==0
			Object.Value2-=1536
		else
			Object.Value2+=1536
		endif
		Object.Value5+=6240
		Object.XPos+=Object.Value2
		Object.YPos+=Object.Value5
		if Object.OutOfBounds==1
			Object.Type=TypeName[BlankObject]
			Object.Priority=0
		endif
		break

	endswitch
endevent


event ObjectDraw
	DrawSprite(0)
endevent


event ObjectStartup
	LoadSpriteSheet("RRZ/Objects.gif")
	SpriteFrame(-8,-8,16,16,156,76)
endevent

event RSDK
	LoadSpriteSheet("Global/Display.gif")
	SetEditorIcon(Icon0,SingleIcon,-16,-16,32,32,1,143)
endevent
